<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客详情页</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/detail.css">

</head>

<body>
<div class="nav">
    <img src="https://tse3-mm.cn.bing.net/th/id/OIP-C.wb9I_pEZGVG3RIQtuLCXNQHaHZ?w=181&h=180&c=7&r=0&o=7&dpr=1.3&pid=1.7&rm=3" alt="">
    <span class="blog-title">我的博客系统</span>
    <div class="space"></div>
    <a class="nav-span" href="blog_list.html">主页</a>
    <a class="nav-span" href="blog_edit.html">写博客</a>
    <a class="nav-span" href="#" onclick="logout()">注销</a>
</div>

<div class="container">
    <div class="left">
        <div class="card">
            <img src="pic/doge.jpg" alt="">
            <h3 class="name">比特汤老师</h3>
            <a class="git" href="#">GitHub 地址</a>
            <div class="row">
                <span>文章</span>
                <span>分类</span>
            </div>
            <div class="row">
                <span>2</span>
                <span>1</span>
            </div>
        </div>
    </div>
    <div class="right">
        <div class="content">
            <div class="title"></div>
            <div class="date"></div>
            <div class="detail"></div>
            <div class="operating"></div>
        </div>

    </div>
</div>

<!-- 引入 editor.md 的依赖 -->
<link rel="stylesheet" href="blog-editormd/css/editormd.css"/>
<script src="js/jquery.min.js"></script>
<script src="blog-editormd/lib/marked.min.js"></script>
<script src="blog-editormd/lib/prettify.min.js"></script>
<script src="blog-editormd/editormd.js"></script>
<script src="js/common.js"></script>
<script>
    //设置文章数,从本地取
    getBlog();

    function getBlog() {
        $.ajax({
            type: "GET",
            url: "blog/selectBlogById" + location.search,
            success: function (result) {
                console.log(result);
                if (result.code == "Success") {
                    var blog = result.data;
                    console.log(blog);
                    $(".title").html(blog.title);
                    $(".date").html(blog.updateTime);
                    $(".detail").html(blog.content);
                }
            },
            error: function (result) {
                console.log(result);
                if (result.status == 401) {
                    location.assign("blog_login.html");
                }
            }
        })
    }

    //显示博客作者信息
    var userUrl = "/user/selectUserInfoByBlogId" + location.search;
    $.ajax({
        type: "GET",
        url: userUrl,
        success: function (result) {
            if (result.code == "Success") {
                var user = result.data;
                $(".name").html(user.userName);
                $(".git").attr("href", user.githubUrl);
                $.ajax({
                    type: "GET",
                    url: "blog/selectBlogCount"+location.search,
                    success: function (result) {
                        console.log(result);
                        if (result.code == "Success") {
                            $(".row:eq(1) span:eq(0)").html(result.data);
                        }
                    }
                })
                //更新文章数/////////////////////////////////////
            }
        }
    })

    $.ajax({
        type: "GET",
        url: "user/isLoginByBlogId" + location.search,
        success: function (result) {
            console.log(result);
            if (result.code == "Success" && result.data == true) {
                var html = "";
                html += '<button onClick="window.location.href=\'blog_update.html' + location.search + '\'">编辑</button>';
                html += '<button onclick="deleteBlog()">删除</button>';
                $(".content .operating").html(html);
            }
        }


    })


    function deleteBlog() {
        if (confirm("确定要删除吗？")) {
            $.ajax({
                type: "DELETE",
                url: "blog/deleteBlogByBlogId" + location.search,
                success: function (result) {
                    console.log(result);
                    if (result.code == "Success") {
                        alert("删除成功");
                        location.assign("blog_list.html");
                    } else {
                        alert(result.errMessage);
                    }
                },
                error: function (result) {
                    console.log(result);
                    if (result.status == 401) {
                        location.assign("blog_login.html");
                    }
                }
            })
        }
    }
</script>
</body>

</html>